package com.hyjy.music.fragment

import android.graphics.Color
import com.hyjy.music.base.BaseFragment
import android.os.Bundle
import android.util.Log
import com.hyjy.music.R
import android.view.LayoutInflater
import com.hyjy.music.bean.MusicBean
import com.hyjy.music.databinding.FragmentShowLrcBinding
import com.hyjy.music.TableLrcHelper
import com.hyjy.music.util.*

/**
 * 歌词Fragment
 */
class LrcFragment : BaseFragment<FragmentShowLrcBinding>() {
    companion object {
        fun newInstance(): LrcFragment {
            val fragment = LrcFragment()
            val args = Bundle()
            fragment.arguments = args
            return fragment
        }
    }

    private lateinit var mainFragment: MainFragment
    private lateinit var lrcColors: Array<String>
    private var musicBean: MusicBean? = null
    private var tableLrcPositionMap = mutableMapOf<String, Int>()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        mainFragment = requireParentFragment() as MainFragment
        lrcColors = resources.getStringArray(R.array.lrc_color)
    }

    override fun initViewBinding(inflater: LayoutInflater): FragmentShowLrcBinding {
        return FragmentShowLrcBinding.inflate(inflater)
    }

    override fun initData() {
        // 回显字体颜色
        val clickPosition = getInt(KEY_LRC_COLOR, 0)
        val color = Color.parseColor(lrcColors[clickPosition])
        changeLrcColor(color)
        // 回显字体大小
        val size = getInt(KEY_LRC_SIZE, 0)
        changeLrcSize(size)
        // 是否匀速滚动
        val uniform = getBoolean(KEY_SWITCH_STATUS_ROLL, false)
        setUniform(uniform)
        // 监听歌词View手势滑动
        binding?.lrcView?.setOnLrcViewScrollListener(::onLrcViewScroll)
    }

    /**
     * 滚动歌词
     *
     * @param url             歌词的路径
     * @param currentPosition 当前播放进度
     * @param totalDuration   播放总时长
     */
    fun rollLrc(url: String, currentPosition: Long, totalDuration: Long) {
        binding?.lrcView?.rollLrcView(url, currentPosition, totalDuration)
        val lightIndex = binding?.lrcView?.getLightIndex()
        if (lightIndex != tableLrcPositionMap[url]) {
            tableLrcPositionMap[url] = lightIndex ?: 0
            showTableLrc()
        }
        // 刷新桌面歌词进度
        val percent = binding?.lrcView?.getSingedPercent()
        TableLrcHelper.refreshLrcProgress(percent ?: 0F)
    }

    private fun onLrcViewScroll(current: Long) {
        mainFragment.myBinder?.let {
            if (it.isMusicPlaying()) {
                it.seekMusicTo(current.toInt())
            }
        }
    }

    fun showTableLrc() {
        Log.d("LrcFragment", "showTableLrc")
        val status = getBoolean(KEY_SWITCH_STATUS, false)
        if (status) {
            val currentLrc = binding?.lrcView?.getCurrentLrc()
            val color = Color.parseColor(lrcColor)
            val title = musicBean?.title ?: ""
            TableLrcHelper.setTitle(title).setData(currentLrc).setColor(color).refreshLrc()
            mainFragment.myBinder?.run {
                TableLrcHelper.setImageResource(if (isMusicPlaying()) R.drawable.ic_baseline_pause_circle else R.drawable.ic_baseline_play_circle)
            }
        }
    }

    /**
     * 颜色修改
     *
     * @param color 字体颜色
     */
    fun changeLrcColor(color: Int) {
        binding?.lrcView?.setLrcColor(color)
    }

    /**
     * 大小修改
     *
     * @param size 字体大小
     */
    fun changeLrcSize(size: Int) {
        binding?.lrcView?.setLrcSize(size)
    }

    // 回显字体颜色
    private val lrcColor: String
        get() {
            // 回显字体颜色
            val clickPosition = getInt(KEY_LRC_COLOR, 0)
            return lrcColors[clickPosition]
        }

    fun setUniform(uniform: Boolean) {
        binding?.lrcView?.setUniform(uniform)
    }

    fun updateUI(musicBean: MusicBean?) {
        // 如果是同一首歌曲,歌词页面无需更新
        if (this.musicBean?.url != musicBean?.url) {
            this.musicBean = musicBean
            // 下面两行代码不要轻易改动
            binding?.lrcView?.setLrcUrl(musicBean?.url ?: "")
            binding?.lrcView?.postInvalidate()
            showTableLrc()
        }
    }
}